# Maintainer: Ricky <rickleaf.wu@gmail.com>

pkgname="emacs"
pkgver=26.3
pkgrel=1
pkgdesc="The extensible, customizable, self-documenting, real-time display editor (msys2)"
url="https://www.gnu.org/software/${pkgname}/"
license=('GPL3')
arch=('i686' 'x86_64')
depends=('ncurses' 'zlib' 'libxml2' 'libiconv' 'libcrypt' 'libgnutls' 'glib2' 'libhogweed')
groups=('editors')
makedepends=('gawk' 'libiconv-devel' 'libxml2-devel' 'libiconv-devel' 'ncurses-devel' 'libcrypt-devel' 'libgnutls-devel')
options=('strip')
source=("https://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.xz"{,.sig}
        'configure.ac.diff')
sha256sums=('4d90e6751ad8967822c6e092db07466b9d383ef1653feb2f95c93e7de66d3485'
            'SKIP'
            '0cb204f2cab4740d27a47a4adc7e4168d6034e86cd522605a85bbd03fb1db59d')
validpgpkeys=('B29426DEFB07724C3C35E5D36592E9A3A0B0F199' '28D3BED851FDF3AB57FEF93C233587A47C207910')

prepare() {
  cd "${pkgname}-${pkgver}"
  patch --binary --forward -p0 < "${srcdir}/configure.ac.diff"
  ./autogen.sh
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  CPPFLAGS="-DNDEBUG"
  CFLAGS="-pipe -O3 -fomit-frame-pointer -funroll-loops"
  LDFLAGS="-s -Wl,-s"
  ./configure \
    --prefix=/usr \
    --build="${CHOST}" \
    --with-x-toolkit=no \
    --with-sound=yes \
    --with-modules \
    --without-compress-install

  make
}

package() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  make DESTDIR="${pkgdir}" install
  mv "${pkgdir}/usr/bin/ctags.exe" \
    "${pkgdir}/usr/bin/ctags-emacs.exe"
  mv "${pkgdir}/usr/share/man/man1/ctags.1" \
     "${pkgdir}/usr/share/man/man1/ctags-emacs.1"
  rm -f "${pkgdir}/usr/share/info/info.info.gz"

  local dir="${pkgdir}/usr/share/${pkgname}"
        dir="${dir}/$(ls -1 ${dir} | grep -E '([0-9]+\.[0-9]+)(\.[0-9]+)?')/src"

  mkdir -p "${dir}"
  cd "${srcdir}/${pkgname}-${pkgver}/src"
  cp *.c *.h *.m "${dir}"
  
}

# TODO:
# Patch `shell-file-name' default in the C source code similarly to
# `source-directory'.
